package com.itheima.controller;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

	@Reference
	private MemberService memberService;

	@Reference
	private SetmealService setmealService;

	@Reference
	private ReportService reportService;

	@RequestMapping("/getMemberReport")
	public Result getMemberReport() {
		//查询月份和每个月份对应的会员数

		try {
			Map map = memberService.queryMonthAndMemberCount();
			return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
		}

	}


	@RequestMapping("/getSetmealReport")
	public Result getSetmealReport() {

		try {
			Map map = setmealService.querySetmeal();
			return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
		}

	}

	@RequestMapping("/getBusinessReportData")
	public Result getBusinessReportData() {

		try {
			Map map = reportService.getBusinessReportData();
			return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
		}


	}

	@RequestMapping("/exportBusinessReport")
	public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
		try {
			//先远程调用获取运营统计数据方法
			Map businessReportData = reportService.getBusinessReportData();
			String reportDate = (String) businessReportData.get("reportDate");
			Integer todayNewMember = (Integer) businessReportData.get("todayNewMember");
			Integer totalMember = (Integer) businessReportData.get("totalMember");
			Integer thisWeekNewMember = (Integer) businessReportData.get("thisWeekNewMember");
			Integer thisMonthNewMember = (Integer) businessReportData.get("thisMonthNewMember");
			Integer todayOrderNumber = (Integer) businessReportData.get("todayOrderNumber");
			Integer todayVisitsNumber = (Integer) businessReportData.get("todayVisitsNumber");
			Integer thisWeekOrderNumber = (Integer) businessReportData.get("thisWeekOrderNumber");
			Integer thisWeekVisitsNumber = (Integer) businessReportData.get("thisWeekVisitsNumber");
			Integer thisMonthOrderNumber = (Integer) businessReportData.get("thisMonthOrderNumber");
			Integer thisMonthVisitsNumber = (Integer) businessReportData.get("thisMonthVisitsNumber");
			List<Map> hotSetmeal = (List<Map>) businessReportData.get("hotSetmeal");

			//获取报告模板文件的路径
			String realPath = request.getSession().getServletContext().getRealPath("report_templat.xls");
			//创建输入流对象
			FileInputStream fileInputStream = new FileInputStream(new File(realPath));
			//创建XSSF工作簿对象
			HSSFWorkbook xssfWorkbook = new HSSFWorkbook(fileInputStream);
			//获得工作表对象
			HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
			//
			System.out.println(reportDate);
			sheet.getRow(2).getCell(5).setCellValue(reportDate);
			//
			sheet.getRow(4).getCell(5).setCellValue(todayNewMember);
			sheet.getRow(4).getCell(7).setCellValue(totalMember);
			//
			sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
			sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
			//
			sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);
			sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
			//
			sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
			sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
			//
			sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
			sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);
			//
			BigDecimal proportion1 = (BigDecimal) hotSetmeal.get(0).get("proportion");
			String prop1 = proportion1.toString();
			BigDecimal proportion2 = (BigDecimal) hotSetmeal.get(1).get("proportion");
			String prop2 = proportion2.toString();
			BigDecimal proportion3 = (BigDecimal) hotSetmeal.get(2).get("proportion");
			String prop3 = proportion3.toString();
			BigDecimal proportion4 = (BigDecimal) hotSetmeal.get(3).get("proportion");
			String prop4 = proportion4.toString();

			//
			sheet.getRow(12).getCell(4).setCellValue((String) hotSetmeal.get(0).get("name"));
			sheet.getRow(12).getCell(5).setCellValue((Long) hotSetmeal.get(0).get("setmeal_count"));
			sheet.getRow(12).getCell(6).setCellValue(prop1);
			sheet.getRow(12).getCell(7).setCellValue("无");
			//
			sheet.getRow(13).getCell(4).setCellValue((String) hotSetmeal.get(1).get("name"));
			sheet.getRow(13).getCell(5).setCellValue((Long) hotSetmeal.get(1).get("setmeal_count"));
			sheet.getRow(13).getCell(6).setCellValue(prop2);
			sheet.getRow(13).getCell(7).setCellValue("无");
			//
			sheet.getRow(14).getCell(4).setCellValue((String) hotSetmeal.get(2).get("name"));
			sheet.getRow(14).getCell(5).setCellValue((Long) hotSetmeal.get(2).get("setmeal_count"));
			sheet.getRow(14).getCell(6).setCellValue(prop3);
			sheet.getRow(14).getCell(7).setCellValue("无");
			//
			sheet.getRow(15).getCell(4).setCellValue((String) hotSetmeal.get(3).get("name"));
			sheet.getRow(15).getCell(5).setCellValue((Long) hotSetmeal.get(3).get("setmeal_count"));
			sheet.getRow(15).getCell(6).setCellValue(prop4);
			sheet.getRow(15).getCell(7).setCellValue("无");
			//通过输出流进行文件下载
			ServletOutputStream outputStream = response.getOutputStream();
			response.setContentType("application/vds.ms-excel");
			//设置文件下载方式
			response.setHeader("content-Disposition", "attachment;filename=report.xls");
			xssfWorkbook.write(outputStream);

			outputStream.flush();
			outputStream.close();
			xssfWorkbook.close();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "导出运营统计数据失败",null);
		}


	}
}
